home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / ti3201.zip / TI3201.ASC < prev   
Text File  |  1997-03-02  |  8KB  |  226 lines

  1.    NUMBER  :  3201
  2.   PRODUCT  :  Delphi
  3.   VERSION  :  All
  4.        OS  :  Windows/Win32
  5.      DATE  :  February 26, 1997                       
  6.  
  7.     TITLE  :  How to use a string table resource
  8.  
  9. Stringtable resources are a very useful tool when your
  10. application must store a large number of strings for use
  11. at runtime. While you may be tempted to directly embed
  12. strings into your executable, using a stringtable resource
  13. offers two advantages: 1) The strings contained in the
  14. stringtable do not consume memory until they are loaded
  15. by your application. 2) Stringtables are easily edited,
  16. providing an easy path to internationaly localized
  17. versions of your application.
  18.  
  19. Stringtables are compiled into a ".res" file that is
  20. attached to your application's exe file at build time.
  21. Even after you distribute your appliaction, the stringtable
  22. contained in your application's exe file can be edited with
  23. a resource editor. My favorite resource editor is Borland's
  24. Resource Workshop that ships with the RAD pack. It can produce
  25. and edit both 16 and 32 bit resources that are self contained,
  26. standalone, or embedded in a .exe or .dll in full WYSIWYG
  27. fashion.
  28.  
  29. It's worth noting that all versions of Delphi ship with the
  30. Borland Resource Command Line Compiler (BRCC.EXE and BRCC32.EXE),
  31. and can be found in Delphi's Bin directory.
  32.  
  33. For our example, we will build an internationalized application
  34. that displays two buttons. The buttons will have captions for
  35. "Yes" and "No" presented in English, Spanish, and Swedish.
  36.  
  37. It's worth noting that if you want to build international
  38. applications using Delphi, you should take a look at Borland's
  39. Delphi Translation Suite and Language Pack software. These
  40. packages can make porting your application to international
  41. markets a snap!
  42.  
  43.  
  44. Example:
  45.  
  46. We first must create a text file containing our string
  47. resources in the applications build directory. You may
  48. name the file anything you wish, so long as it has the
  49. file extension ".rc" and the filename without the extension
  50. is not the same as any unit or project filename. This is
  51. very important, as Delphi also will create a number of
  52. resource files for your project automatically.
  53.  
  54. Here is the contents of the .rc file for our example. It
  55. contains the words "Yes" and "No" in English, Spanish,
  56. and Swedish:
  57.  
  58. STRINGTABLE
  59. {
  60.  1, "&Yes"
  61.  2, "&No"
  62.  17, "&Si"
  63.  18, "&No"
  64.  33, "&Ja"
  65.  34, "&Nej"
  66. }
  67.  
  68.  
  69. The file starts with the key word stringtable denoting that
  70. a string table resource will follow. Enclosed in the
  71. curly braces are the strings. Each string is listed by it's
  72. index identifier, followed by the actual string data in
  73. quotes. Each string may contain up to 255 characters. If you
  74. need to use a non-standard character, insert the character
  75. as a backslash character followed by the octal number of
  76. the character you wish to insert. The only exception is
  77. when you want to embed a backslash character, you will need
  78. to use two backslashes. Here are two examples:
  79.  
  80. 1, "A two\012line string"
  81.  
  82. 2, "c:\\Borland\\Delphi"
  83.  
  84. The Index numbers that you use are not important to the
  85. resource compiler. You should keep in mind that string
  86. tables are loaded into memory in 16 string segments.
  87.  
  88. To compile the .rc file to a .res file that can be linked
  89. with your application, simply type on the dos command line
  90. the full path to the resource compiler, and the full path
  91. to the name of the .rc file to compile. Here is an example:
  92.  
  93. c:\Delphi\Bin\brcc32.exe c:\Delphi\strtbl32.rc
  94.  
  95. When the compiler is finished, you should have a new file
  96. with the same name as the .rc file you've compiled, only
  97. with an extension of ".res".
  98.  
  99. You can link the resource file with your application simply
  100. by adding the following statement to your application's
  101. code, substituting the name of your resource file:
  102.  
  103. {$R ResFileName.RES}
  104.  
  105. Once the .res file is linked to your program, you can load
  106. the resource from any module, even if you specified
  107. the $R directive in the implementation section of a
  108. different unit.
  109.  
  110. Here is an example of using the Windows API function
  111. LoadString(), to load the third string contained in
  112. a string resource into a character array:
  113.  
  114.   if LoadString(hInstance,
  115.                 3,
  116.                 @a,
  117.                 sizeof(a)) <> 0 then ....
  118.  
  119. In this example, the LoadString() function accepts the
  120. hInstance of the module containing the resource, the
  121. string index to load, the address of the character array
  122. to load the string to, and the size of the character array.
  123. The LoadString function returns the number of characters
  124. that where actually loaded not including the null terminator.
  125. Be aware that this can differ from the number of bytes
  126. loaded when using unicode.
  127.  
  128.  
  129. Here is a complete example of creating an international
  130. application with Borland's Delphi. The application is
  131. compatible with both 16 and 32 bit versions of Delphi.
  132.  
  133. To do this, you will need to create two identical .rc
  134. files, one for the 16 bit version, and the other for the
  135. 32 bit version, since the resources needed for each 
  136. platform are different. In this example. we will create
  137. one file named STRTBL16.rc and another called STRTBL32.rc.
  138. Compile the STRTBL16.rc file using the BRCC.exe compiler
  139. found in Delphi 1.0's bin directory, and compile STRTBL32.rc
  140. using the BRCC32.exe compiler found in Delphi 2.0's bin
  141. directory.
  142.  
  143. We have taken into account the language that Windows
  144. is currently using at runtime. The method for getting
  145. this information differs under 16 and 32 bit Windows.
  146. To make the code more consistant, we have borrowed the
  147. language constants from the Windows.pas file used in 32
  148. bit versions of Delphi.
  149.  
  150.  
  151.  
  152. {$IFDEF WIN32}
  153.    {$R STRTBL32.RES}
  154. {$ELSE}
  155.    {$R STRTBL16.RES}
  156.    const LANG_ENGLISH = $09;
  157.    const LANG_SPANISH = $0a;
  158.    const LANG_SWEDISH = $1d;
  159. {$ENDIF}
  160.  
  161.  
  162. function GetLanguage : word;
  163. {$IFDEF WIN32}
  164. {$ELSE}
  165.   var
  166.     s : string;
  167.     i : integer;
  168. {$ENDIF}
  169. begin
  170. {$IFDEF WIN32}
  171.   GetLanguage := GetUserDefaultLangID and $3ff;
  172. {$ELSE}
  173.   s[0] := Char(GetProfileString('intl',
  174.                                 'sLanguage',
  175.                                 'none',
  176.                                 @s[1],
  177.                                 sizeof(s)-2));
  178.   for i := 1 to length(s) do
  179.     s[i] := UpCase(s[i]);
  180.   if s = 'ENU' then GetLanguage := LANG_ENGLISH else
  181.   if s = 'ESN' then GetLanguage := LANG_SPANISH else
  182.   if s = 'SVE' then GetLanguage := LANG_SWEDISH else
  183.     GetLanguage := LANG_ENGLISH;
  184. {$ENDIF}
  185. end;
  186.  
  187.  
  188. procedure TForm1.FormCreate(Sender: TObject);
  189. var
  190.   a : array[0..255] of char;
  191.   StrTblOfs : integer;
  192. begin
  193.  
  194.  {Get the current language and stringtable offset}
  195.   case GetLanguage of
  196.     LANG_ENGLISH : StrTblOfs := 0;
  197.     LANG_SPANISH : StrTblOfs := 16;
  198.     LANG_SWEDISH : StrTblOfs := 32;
  199.    else
  200.     StrTblOfs := 0;
  201.   end;
  202.  
  203.  {Load language dependent "Yes" and set the button caption}
  204.   if LoadString(hInstance,
  205.                 StrTblOfs + 1,
  206.                 @a,
  207.                 sizeof(a)) <> 0 then
  208.     Button1.Caption := StrPas(a);
  209.  
  210.  {Load language dependent "No" and set the button caption}
  211.   if LoadString(hInstance,
  212.                 StrTblOfs + 2,
  213.                 @a,
  214.                 sizeof(a)) <> 0 then
  215.     Button2.Caption := StrPas(a);
  216. end;
  217.  
  218. <end of ti>
  219.  
  220.  
  221.  
  222. DISCLAIMER: You have the right to use this technical information
  223. subject to the terms of the No-Nonsense License Statement that
  224. you received with the Borland product to which this information
  225. pertains.
  226.